﻿using Microsoft.Extensions.DependencyInjection;
using System.Configuration;
using System.Data;
using System.Windows;
using ZeroBug.EasyFlow.Communication;
using ZeroBug.EasyFlow.Core;
using ZeroBug.EasyFlow.Machine;
using ZeroBug.EasyFlow.Motion;

namespace EasyFlow
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public new static App Current => (App)Application.Current;
        public IServiceProvider Services { get; }
        public App()
        {
            Services = ConfigureServices();
        }
        private static IServiceProvider ConfigureServices()
        {
            var services = new ServiceCollection();

            #region Log
            services.AddSingleton<LogHelper>(LocalMachine.Services.GetService<LogHelper>()!);
            #endregion

            #region Assembly
            services.AddSingleton<AssemblyHelper>(LocalMachine.Services.GetService<AssemblyHelper>()!);
            #endregion

            #region Variable
            services.AddSingleton<VariableManager>();
            #endregion

            #region Flow
            //注入逻辑控制单元
            services.AddSingleton<FlowManager>();
            #endregion

            return services.BuildServiceProvider();
        }
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            LocalMachine.Services.GetService<LogHelper>()?.Log(LogSource.App, "Load assembly...", MessageLevel.Log);
            LocalMachine.Services.GetService<AssemblyHelper>()?.LoadAssembly("", new string[] { "Pluggins", "StepLibs" });

            LocalMachine.Services.GetService<LogHelper>()?.Log(LogSource.App, "Load machine...", MessageLevel.Log);
            LocalMachine.Services.GetService<MachineResource<Axis, IAxis>>()?.Load("Axis");
            LocalMachine.Services.GetService<MachineResource<Sensor, IDigitalIO>>()?.Load("Sensor");
            LocalMachine.Services.GetService<MachineResource<Cylinder, IDigitalIO>>()?.Load("Cylinder");
            LocalMachine.Services.GetService<MachineResource<CommunicationClient, ICommunicationClient>>()?.Load("Comm");

            //Variable a = new Variable("var1", "12.5", VariableType.Numeric);
            //Variable b = new Variable("var2", "3", VariableType.Numeric);
            //Services.GetService<VariableManager>()!.AddVariable(a, VariableFrom.Global);
            //Services.GetService<VariableManager>()!.AddVariable(b, VariableFrom.Global);

            //b.Expression = "([var2] *2 -(10-9+3+(4*2)))+ (4* [var1]-2)";


            //CommunicationClient client1 = LocalMachine.Services.GetService<MachineResource<CommunicationClient, ICommunicationClient>>()!.Value["tcpClient1"];

            //client1.OperatorName = "TCPClient";
            //client1.Description = "tcpClient1";
            //client1.SetOperator(LocalMachine.Services.GetService<AssemblyHelper>()?.GetInstance<ICommunicationClient>()["TCPClient"]);
            //client1.Logger = LocalMachine.Services.GetService<LogHelper>();

            //TCPClientConfig config1 = new TCPClientConfig();
            //config1.IpAddress = "192.168.201.19";
            //config1.Port = 8234;
            //client1.Config = config1;

            //LocalMachine.Services.GetService<MachineResource<CommunicationClient, ICommunicationClient>>()?.Value.Add("tcpClient1", client1);
            //LocalMachine.Services.GetService<MachineResource<CommunicationClient, ICommunicationClient>>()?.Save("Comm");

            //client1.Connect();

            //SendPackage<byte[]> sp = new SendPackage<byte[]>()
            //{
            //    StartPattern = new byte[1] { 0x3a },
            //    EndPattern = new byte[2] { 0x0d, 0x0a },
            //    Message = new byte[3] { 0x01, 0x02, 0x03 },
            //};
            //client1.Send(sp);

            //while (!sp.ReceivingFlag)
            //{
            //    Thread.Sleep(20);
            //}
            //MessageBox.Show(sp.ReceivedData.ToString());

            //string aa = b[0];
        }
    }

}
